package logic

import (
	"automated-modeling/automatedrpc/automatedrpc"
	"automated-modeling/automatedrpc/internal/svc"
	"automated-modeling/models/mysql"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type CategoriesAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCategoriesAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CategoriesAddLogic {
	return &CategoriesAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CategoriesAddLogic) CategoriesAdd(in *automatedrpc.CategoriesAddRequest) (*automatedrpc.CategoriesAddResponse, error) {
	// todo: add your logic here and delete this line
	var categories mysql.Categories
	categories = mysql.Categories{
		Name:    in.Name,
		Image:   in.Image,
		Profile: in.Profile,
		UserId:  int(in.UserId),
	}
	err := mysql.DB.Debug().Create(&categories).Error
	if err != nil {
		return nil, fmt.Errorf("添加失败")
	}

	return &automatedrpc.CategoriesAddResponse{Id: int64(categories.ID)}, nil
}
